/**
 * 自定义装修页面
 */
import {
  api,
  util,
} from '../js/util'
import authorize from '../js/modules/authorize'
import { config, share } from "../js/modules/gloab";
import BasePage from '../js/common/base-page'
import {
  router,
  pagesInfo
} from '../js/router.js'
import {
  theme
} from '../js/modules/theme'
const app = getApp()


BasePage({
  pageKey: "customIndex",
  validateLogin: false,
  /**
   * 页面的初始数据
   */
  data: {
    pageId:0,
    height:0,
    mainColor:'',//主题色
    Currency:'',//货币符号
    NoLoginShowPrice:false,//
    isLogin:true,//判断是否登录
    hasFooter:true,   //是否有底部导航
    tabindex:-1,
    isIphoneX:app.globalData.isIphoneX,
    showLive:false,  //显示直播
    showMinService:false,  //显示客服
    showPhone:false,  //显示电话
    phone:'',  //电话号码
    LoginType: 0,//登录方式
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var h = wx.getSystemInfoSync().windowHeight;
    this.setData({
      height:h
    })
    var pageId = options.page;
    var that = this;
    this.indexWidgetsComponent = this.selectComponent("#indexWidgetsComponent");

    if (options.page==undefined || options.page <= 0) {
      //数据加载错误
      router.switchTab({
        path: "index"
      });
      return;
    }

    this.indexWidgetsComponent.setData({
      pageId: pageId
    },function(){
      that.indexWidgetsComponent.loadTemplates();
    });
    this.getIndexPageConfig(pageId)
    this.getWholesaleConfig();
    this.getShareInfo()
    app.initTabBar();
    this.setData({
      hasFooter:true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.setNavigationBarTitle({
      title: api.title
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      tabindex:0
    })
    //判断是否登录
    if (app.dns.proprietorId == 0 && !authorize.isLogin('store')) {
      authorize.redirect('store');
      return;
    }
    this.setData({
      mainColor:api.titleColor||"#e56b14",
      Currency:api.Currency || '￥',
      // isLogin: authorize.isLogin('store')
    });
    // to fixed: 直接设置isLogin：authorize.isLogin('store') 会导致组件内isLogin固定变为true
    // 现在先改为直接修改组件内isLogin属性
    this.indexWidgetsComponent.setData({
      isLogin: authorize.isLogin('store')
    })
  },
   /**
   * 获取分享信息
   */
  getShareInfo:function(){
    let that = this;
    let param = {
      Proprietor:app.dns.proprietor,
      ProprietorId:app.dns.proprietorId,
      Type:'mall'
    }
    share.getinfo(param,false,function(res){
      let shareObj = {};
      shareObj.title = res.Title || "邀请加入";
      shareObj.img = res.ImgSrc!=''?api.imgDomain+res.ImgSrc:''; //.replace('http://','https://')
      that.setData({
        shareObj:shareObj
      })
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.indexWidgetsComponent.initPropertity();
    this.indexWidgetsComponent.loadTemplates();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.indexWidgetsComponent.loadMorePageWidgets();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let storeInfo = authorize.getEntity();
    let scene="";
    if(storeInfo){
      scene=storeInfo.fkFlag+"_"+storeInfo.fkId+"_"+storeInfo.manager.ManagerId;
    }
    let pages = getCurrentPages()
    let currentPage = pages[pages.length - 1] || {}
    let options = currentPage.options || {}
    let path = pagesInfo.customIndex.url + `?scene=${scene}&page=${options.page}&p=${options.p}`;
    let {shareObj} = this.data;
    return {
      title: shareObj.title,
      path: path
    }
  },
  
  /**加载商品类的配置项参数 */
  getWholesaleConfig() {
    var that = this;
    var fkid = app.dns.proprietorId;
    var fkflag = app.dns.proprietor;
    config.getWholesaleConfigs(
      fkid,
      fkflag,
      "Wholesale",
      function (model) {
        that.setData({
          showLive:model.IsShowLive,
          showMinService:model.IsShowMinService,
          showPhone:model.IsShowPhone,
          phone:model.ServicePhone,
          IsShowJizhipai:model.IsShowJizhipai,
          distributorCustomNo:model.distributorCustomNo,
          LoginType: model.LoginType || 0
        })
        that.indexWidgetsComponent.setData({
          quantityDigits: model.QuantityDigits,
          priceDigits: model.PriceDigits,
          enableZeroStockBuy: model.EnableZeroStockBuy,
          NotShowProductNoneOffered: model.NotShowProductNoneOffered, // 是否不显示无货商品按钮
          spectStyle: model.GoodsDisplayMode,
          isScanty: model.IsScanty, //是否允许缺货登记
          NoLoginShowPrice:model.NoLoginShowPrice,//0：建议零售价 1：不显示价格
          BGColorForMobile: model.BGColorForMobile
        });
        wx.setNavigationBarTitle({
          title: model.SiteName
        });
      });
  },
  getIndexPageConfig (pageId) {
    let that = this
    theme.getIndexPageConfig(pageId, function (res) {
      that.indexWidgetsComponent.setData({
        indexPageBackground: res.BGColor || '' // 首页内设置的背景颜色
      })
    })
  },
})